'use strict';
module.exports = {
	/**
	 * 查询账本所有记账数据
	 * @url client/accounting/record/kh/getBookDataList 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _, $ } = util;
		let { uid, accountBook,pagination } = data;
		let res = { code: 0, msg: "查询成功" };
		// 业务逻辑开始-----------------------------------------------------------
		if(accountBook == null ){
			return { code : 1001, msg : '参数错误' };
		}
		
		let {_id} = accountBook;
		if(pubFun.isEmpty(_id)){
			return { code : 1001, msg : '参数错误' };
		}
		
		let dataRtn = await vk.baseDao.selects({
		  dbName: "db-book-relation-data",
		 pageIndex: pagination.pageIndex, // 查询第几页
		 pageSize: pagination.pageSize, // 每页多少条数据
		  getCount: false,
		  // 主表where条件
		  whereJson: {
			  bookId:_.eq(_id)
		  },
		  fieldJson: {
			  _id:true,
			  dataId:true,
			  bookId:true,
		  },
		  // 主表排序规则
		 sortArr: [{ name:"createTime", type:"desc" }],
		  // 副表列表
		  foreignDB: [
		    {
		      dbName: "db-book-keeping-data",
		  	  // 主表外键名
		      localKey: "dataId",
		      foreignKey: "_id",
		      as: "dataInf",
		      limit: 1,
			  // 副表列表
			  foreignDB: [
			    {
			      dbName: "uni-id-users",
			  	  // 主表外键名
			      localKey: "uid",
			      foreignKey: "_id",
			      as: "userInf",
			      limit: 1,
				  fieldJson: {
					  avatar: true,
					  nickname: true,
				  },
			    },	
			  ],
		    },	
		  ],
		});
		
		if(pubFun.isNotEmpty(dataRtn) && pubFun.isNotEmpty(dataRtn.total) && dataRtn.total > 0) {
			res.data = dataRtn
			dataRtn.data = dataRtn.rows;
		}
		else{
			res.data = []
		}
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}